# Maintainer: Martell Malone <martellmalone@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=git

pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.9.4
pkgrel=1
pkgdesc="The fast distributed version control system (mingw-w64)"
arch=('any')
url="https://git-scm.com/"
license=('GPL2')

makedepends=('python2' 'less' 'openssh') # 'rsync')

depends=("${MINGW_PACKAGE_PREFIX}-curl"
         "${MINGW_PACKAGE_PREFIX}-expat>=2.0"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-tcl"
         "perl-Error"
         "perl>=5.14.0"
         "perl-Authen-SASL"
         "perl-libwww"
         "perl-MIME-tools"
         "perl-Net-SMTP-SSL"
         "perl-TermReadKey"
         "winpty-git")

options=(!libtool strip staticlibs)

_sillyname=$pkgver.msysgit.1

source=("https://github.com/msysgit/git/archive/v$_sillyname.tar.gz"
        "0000-Add-SEARCH_LIBS-to-GIT_CHECK_FUNC.patch"
        "0001-Don-t-redefine-_ReadWriteBarrier-in-malloc.c.h.patch"
        "0002-Undefine-FORCEINLINE-on-MinGW-w64-in-malloc.c.h.patch"
        "0003-Fix-BASIC_LDFLAGS-and-COMPAT_CFLAGS-for-64bit-MinGW-.patch"
        "0004-Also-look-in-winsock2.h-for-sockaddr_storage-and-loo.patch"
        "0005-Don-t-redefine-some-things-MinGW-w64-provides.patch"
        "0006-Include-ntdef.h-on-MinGW-w64-for-REPARSE_DATA_BUFFER.patch"
        "0007-Include-winsock2.h-and-not-netdb.h-on-MinGW-w64.patch"
        "0008-fix-mingw64-compat-wide-char.patch"
        "0009-Change-inline-definition-of-fork-for-mingw64.patch"
        "0010-Use-GIT_CHECK_FUNC-for-socket-and-basename.patch"
        "0011-Use-GIT_CHECK_FUNC-for-regcomp-regex.patch"
        "0012-Use-system-dirent.h-on-MinGW-w64.patch"
        "0013-Make-pthread-wrapper-only-for-mingw.patch"
        "0014-Do-not-compile-dirent-on-mingw64.patch"
        "0015-Alter-fscache-for-mingw64.patch"
        "0016-Use-time.h-versions-of-gmtime-localtime-_r.patch")

sha256sums=('d5b042e88d8592cac1095d44cd623def4d6bfa2ebcc64cdc4716e6fd42cba85c'
            'c0572e2308b827d6c658915dda470c05a8ed7456573a87549057195f667de6a7'
            '7cd9b9f822a3c4b5ebf088af1d01ceff918c5e74a0590decec6d9ef3a8c5d0cc'
            '19b39c27dd007e2d73ef623f3d9db910bffccac00c30c1df3be9922d9ce8d718'
            'cd778d56ad03f863fa230072299b36f0b1c2e49c7914fa65a3fcb8d7bd8f0e07'
            'ef3021be142259cdecde7f9632a10102b0f7d570eb6e16b7da45050775b02a35'
            'c48654b94c1369c3f939689634dfb95382a4b9464f699d9e8afd53dc8ded625d'
            '331e576532924e6a5ae99a7b87e5ee0a3c4687ccc209e2eaee710621152dd2de'
            '4b9216db5dce6803174075148fa2b04728c06865a687e258bd134a212533c52e'
            '82c141851bca5b5b1695204a129827c99ca05d574c89057ec035fbfd42c141f4'
            '13de3242d88dd595f6bf5597515b118049f7b0b59a445feddcbbf34b3165037d'
            'bf7395ade4a91b5ef5c8171894a2a8c5ee4b08296cb102caee3aa741b35544f7'
            '1e8d4cc26b8c90924d36ddc5638208ddbf95e7c8adcc29afdc6356214a941326'
            '2ee4158d70cc211dcc4b1857eeeaf62c54afe96e00912b1d6c810752afb9bb38'
            'eed3357b4b459d113c524158156f759916d895373ff42c826c137ca8784433fa'
            'ca1cc62c2b1a57f033e3c234089e7f7fdfe7d622f2aac687deb6a3099b96b605'
            '45fa4b2996ff7be3ee1ac320a61a207c4531f3e403d487435e58f821e4c96c8c'
            'dc97b4ec2263e6b5142e339e3e317dc1f10b9265ed5175bc8e026df509debdb0')

prepare() {

  export PYTHON_PATH=/usr/bin/python2
  cd "$srcdir"/$_realname-$_sillyname

  [ -f compat/win32/dirent-mingw.h ]  && rm -f compat/win32/dirent-mingw.h
  [ -f compat/win32/pthread-mingw.h ] && rm -f compat/win32/pthread-mingw.h

  patch -p1 < "${srcdir}"/0000-Add-SEARCH_LIBS-to-GIT_CHECK_FUNC.patch
  patch -p1 < "${srcdir}"/0001-Don-t-redefine-_ReadWriteBarrier-in-malloc.c.h.patch
  patch -p1 < "${srcdir}"/0002-Undefine-FORCEINLINE-on-MinGW-w64-in-malloc.c.h.patch
  patch -p1 < "${srcdir}"/0003-Fix-BASIC_LDFLAGS-and-COMPAT_CFLAGS-for-64bit-MinGW-.patch
  patch -p1 < "${srcdir}"/0004-Also-look-in-winsock2.h-for-sockaddr_storage-and-loo.patch
  patch -p1 < "${srcdir}"/0005-Don-t-redefine-some-things-MinGW-w64-provides.patch
  patch -p1 < "${srcdir}"/0006-Include-ntdef.h-on-MinGW-w64-for-REPARSE_DATA_BUFFER.patch
  patch -p1 < "${srcdir}"/0007-Include-winsock2.h-and-not-netdb.h-on-MinGW-w64.patch
  patch -p1 < "${srcdir}"/0008-fix-mingw64-compat-wide-char.patch
  patch -p1 < "${srcdir}"/0009-Change-inline-definition-of-fork-for-mingw64.patch
  patch -p1 < "${srcdir}"/0010-Use-GIT_CHECK_FUNC-for-socket-and-basename.patch
  patch -p1 < "${srcdir}"/0011-Use-GIT_CHECK_FUNC-for-regcomp-regex.patch
  patch -p1 < "${srcdir}"/0012-Use-system-dirent.h-on-MinGW-w64.patch
  patch -p1 < "${srcdir}"/0013-Make-pthread-wrapper-only-for-mingw.patch
  patch -p1 < "${srcdir}"/0014-Do-not-compile-dirent-on-mingw64.patch
  patch -p1 < "${srcdir}"/0015-Alter-fscache-for-mingw64.patch
  patch -p1 < "${srcdir}"/0016-Use-time.h-versions-of-gmtime-localtime-_r.patch
  autoreconf -fi
}

build() {

  export PYTHON_PATH=/usr/bin/python2
  cd "$srcdir"

  [ -d build-${CARCH} ] && rm -rf build-${CARCH}
  cp -rf $_realname-$_sillyname build-${CARCH}
  cd build-${CARCH}

  ./configure \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --prefix=${MINGW_PREFIX} \
    --libexecdir=${MINGW_PREFIX} \
    --with-editor=vim \
    --with-shell=/usr/bin/sh \
    --with-perl=/usr/bin/perl \
    --htmldir=${MINGW_PREFIX}/share/doc/git/html \
    --mandir=${MINGW_PREFIX}/share/man \
    --with-curl \
    --with-openssl \
    --with-expat \
    --with-iconv

  make INSTALLDIRS=vendor -j1 all V=1
  make -C contrib/subtree prefix=/usr all V=1

}

package() {

  # winpty-git script to invoke console
  mkdir -p "${pkgdir}${MINGW_PREFIX}/bin"
  _exename="git"
  echo "#!/usr/bin/env bash" > "${pkgdir}${MINGW_PREFIX}/bin/${_exename}"
  echo '/usr/bin/winpty "$( dirname ${BASH_SOURCE[0]} )/'${_exename}'.exe" "$@"' >> "${pkgdir}${MINGW_PREFIX}/bin/${_exename}"

  export PYTHON_PATH=/usr/bin/python2
  cd "$srcdir"/build-${CARCH}
  make INSTALLDIRS=vendor DESTDIR="$pkgdir" install
}
